package cn.lql.org.controller;

import cn.lql.basic.util.AjaxResult;
import cn.lql.basic.util.PageList;
import cn.lql.org.domain.Department;
import cn.lql.org.query.DepartmentQuery;
import cn.lql.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
// @GetMapping
// @PostMapping
// @DeleteMapping
// @PutMapping
// @PatchMapping

// 部门接口
//@Controller
//@ResponseBody
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    // 新增和修改部门 put
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    // 删除部门 delete
    @DeleteMapping("/{id}")  // http:// localhost:8080/dept/1
    public AjaxResult deleteById(@PathVariable("id") Long id){
//        很low
//          try {
//            departmentService.delete(id);
//            return new AjaxResult();
//        } catch (Exception e) {
//            e.printStackTrace();
//            AjaxResult result = new AjaxResult();
//            result.setSuccess(false);
//            result.setMessage("操作失败！");
//            return result;
//        }
        // 大神玩的
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    // 查询一个部门 get
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    // 获取所有部门 get
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    // 分页和高级查询  post
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }


     // 批量删除接口
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        // 大神玩的
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    // 获取所有部门 get
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }

}